package com.zx.crowdfunding.constant;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 允许访问的资源
 * @author 郑雪
 * @date 2022-01-01
 */
public class AccessPassResources {

	/**
	 * 允许访问的资源集合
	 */
	public static final Set<String> PASS_RESOURCES_SET = new LinkedHashSet<>();
	
	static {
		PASS_RESOURCES_SET.add("/");
		PASS_RESOURCES_SET.add("/auth/member/to/login/page");
		PASS_RESOURCES_SET.add("/auth/member/do/login");
		PASS_RESOURCES_SET.add("/auth/member/do/logout");
		PASS_RESOURCES_SET.add("/auth/member/to/register/page");
		PASS_RESOURCES_SET.add("/auth/member/do/register");
		PASS_RESOURCES_SET.add("/auth/member/send/short/message.json");
	}
	
	/**
	 * 静态资源
	 */
	public static final Set<String> STATIC_RESOURCES_SET = new LinkedHashSet<>();
	
	static {
		STATIC_RESOURCES_SET.add("bootstrap");
		STATIC_RESOURCES_SET.add("css");
		STATIC_RESOURCES_SET.add("fonts");
		STATIC_RESOURCES_SET.add("img");
		STATIC_RESOURCES_SET.add("jquery");
		STATIC_RESOURCES_SET.add("layer");
		STATIC_RESOURCES_SET.add("script");
		STATIC_RESOURCES_SET.add("ztree");
	}
	
	/**
	 * 判断请求路径是否为静态资源
	 * @param servletPath 当前请求路径
	 * @return true：是静态资源；false：不是静态资源
	 */
	public static boolean judgeCurrentServletPathWetherStaticResource(String servletPath) {
		
		// 1.排除字符串无效的情况
		if(servletPath == null || servletPath.length() == 0) {
			throw new RuntimeException(CrowdFundingConstant.MESSAGE_STRING_INVALIDATE);
		}
		
		// 2.根据“/”拆分ServletPath字符串
		String[] split = servletPath.split("/");
		
		// 3.获取一级路径
		String firstLevelPath = split[1];
		
		// 4.判断是否在静态资源集合中，并返回结果
		return STATIC_RESOURCES_SET.contains(firstLevelPath);
	}
}
